#!/bin/bash
###
# @Author: Logan.Li
# @Gitee: https://gitee.com/attacker
# @email: admin@attacker.club
# @Date: 2025-03-14 10:15:30
# @LastEditTime: 2025-03-14 10:15:35
# @Description: 云监控系统启动脚本
###

# 获取脚本所在目录
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 打印带颜色的信息
print_info() {
    echo -e "${GREEN}[信息] $1${NC}"
}

print_warn() {
    echo -e "${YELLOW}[警告] $1${NC}"
}

print_error() {
    echo -e "${RED}[错误] $1${NC}"
}

# 检查Python环境
check_python() {
    if ! command -v python3 &> /dev/null; then
        print_error "未找到python3，请先安装Python 3.6+"
        exit 1
    fi
}

# 检查配置文件
check_config() {
    local platform=$1
    if [ ! -f "$platform/config.ini" ]; then
        print_warn "$platform/config.ini 不存在"
        if [ -f "$platform/config.ini.example" ]; then
            print_warn "请复制 $platform/config.ini.example 为 config.ini 并修改配置"
        fi
        return 1
    fi
    return 0
}

# 创建并激活虚拟环境
setup_venv() {
    local platform=$1
    cd "$platform"
    
    if [ ! -d "venv" ]; then
        print_info "创建 $platform 虚拟环境..."
        python3 -m venv venv
    fi
    
    source venv/bin/activate
    pip install -r requirements.txt
    cd ..
}

# 运行监控程序
run_monitor() {
    local platform=$1
    cd "$platform"
    
    print_info "启动 $platform 监控..."
    source venv/bin/activate
    python main.py
    deactivate
    cd ..
}

# 主函数
main() {
    print_info "开始启动云监控系统..."
    
    # 检查Python环境
    check_python
    
    # 阿里云监控
    if check_config "aliyun"; then
        setup_venv "aliyun"
        run_monitor "aliyun"
    fi
    
    # 华为云监控
    if check_config "huaweicloud"; then
        setup_venv "huaweicloud"
        run_monitor "huaweicloud"
    fi
    
    # AWS监控
    if check_config "aws"; then
        setup_venv "aws"
        run_monitor "aws"
    fi
    
    print_info "监控系统启动完成！"
}

# 运行主函数
main
